package com.yt.server.utils;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropUtil {
	
	private PropUtil() {
		if (properties == null) {
			loads();
		}
	}

	private static PropUtil util = null;
	private static Object lock = new Object();
	private static String prop = "conf/config.properties";
	private Properties properties = null;

	public static PropUtil instance() {
		if (util == null) {
			synchronized (lock) {
				if (util == null) {
					util = new PropUtil();
				}
			}
		}
		return util;
	}

	private void loads() {
		properties = new Properties();
		InputStream in = null;
		try {
			in = getClass().getClassLoader().getResourceAsStream(prop);
			properties.load(in);
		} catch (Exception e) {
			System.err.println("Error reading conf properties " + e);
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (Exception e) {
			}
		}
	}

	public Properties getProperties(String path) {
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = getClass().getResourceAsStream(path);
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return props;
	}

	public String getProperty(String name) {
		String property = properties.getProperty(name);
		if (property == null) {
			return "";
		} else {
			return property.trim();
		}
	}

	@SuppressWarnings("unchecked")
	public Enumeration<String> propertyNames() {
		return (Enumeration<String>) properties.propertyNames();
	}
}
